package com.xie.smfs.client;

import com.xie.smfs.Smfs;
import com.xie.smfs.handler.KeyInputHandler;
import com.xie.smfs.registry.ModEffects;
import com.xie.smfs.registry.ModItems;
import com.xie.smfs.registry.ModBlocks;
import com.xie.smfs.screen.SecondInventoryScreen;
import com.xie.smfs.common.events.PlayerEvents;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import com.xie.smfs.client.screen.GhostControlScreen;
import com.xie.smfs.registry.ModScreenHandlers;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.effect.StatusEffectInstance;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xie.smfs.registry.ModEntities;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import software.bernie.geckolib.renderer.GeoBlockRenderer;
import com.xie.smfs.client.renderer.CoffinBlockRenderer;
import com.xie.smfs.client.renderer.RedCoffinBlockRenderer;
import com.xie.smfs.client.renderer.GoldCoffinBlockRenderer;
import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry;
import net.fabricmc.fabric.impl.blockrenderlayer.BlockRenderLayerMapImpl;
import net.minecraft.client.render.RenderLayer;
import com.xie.smfs.registry.ModBlockEntities;

public class SmfsClient implements ClientModInitializer {
    public static final String MOD_ID = "smfs";
    public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

    private static KeyBinding openInventoryKey;
    private static KeyBinding toggleGhostDomainKey;


    @Override
    public void onInitializeClient() {
        // 注册实体渲染器
        EntityRendererRegistry.register(ModEntities.FOOD_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.TAITOU_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.DITOU_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.FOG_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.QIAOMEN_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.GHOST_MERCHANT, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.BOX_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.VILLAGER_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.JUMP_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.UNTOUCHABLE_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.DEATH_SIGHT_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.BLOCK_GHOST, GhostRenderer::new);
        EntityRendererRegistry.register(ModEntities.LOST_GHOST, GhostRenderer::new);

        // 注册棺材方块GeckoLib渲染器 - 确保ModBlockEntities已初始化
        BlockEntityRendererRegistry.register(ModBlockEntities.GHOST_COFFIN_BLOCK_ENTITY, CoffinBlockRenderer::new);
        BlockRenderLayerMapImpl.INSTANCE.putBlock(ModBlocks.GHOST_COFFIN, RenderLayer.getTranslucent());
        // 注册红棺材渲染器
        BlockEntityRendererRegistry.register(ModBlockEntities.RED_COFFIN_BLOCK_ENTITY, RedCoffinBlockRenderer::new);
        BlockRenderLayerMapImpl.INSTANCE.putBlock(ModBlocks.RED_COFFIN, RenderLayer.getTranslucent());
        // 注册黄金棺材渲染器
        BlockEntityRendererRegistry.register(ModBlockEntities.GOLD_COFFIN_BLOCK_ENTITY, GoldCoffinBlockRenderer::new);
        BlockRenderLayerMapImpl.INSTANCE.putBlock(ModBlocks.GOLD_COFFIN, RenderLayer.getTranslucent());
        // 注册效果渲染处理器
        EffectRenderHandler.register();

        // 注册厉鬼控制界面
        HandledScreens.register(ModScreenHandlers.GHOST_CONTROL_SCREEN_HANDLER, GhostControlScreen::new);

        // 使用 HandledScreens 注册屏幕
        HandledScreens.register(Smfs.SECOND_INVENTORY_SCREEN_HANDLER, SecondInventoryScreen::new);
        // 注册按键绑定
        openInventoryKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.smfs.secondinventory.open",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_B,
                "category.smfs.general"
        ));

        // 注册鬼域切换按键(K键)
        toggleGhostDomainKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.smfs.ghostdomain.toggle",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_K,
                "category.smfs.general"
        ));

        // 监听按键事件
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            // 处理鬼域切换按键
            while (toggleGhostDomainKey.wasPressed()) {
                if (client.player != null) {
                    com.xie.smfs.manager.GhostDomainManager.toggleGhostDomain(client.player);
                }
            }
        });
    }

    private static boolean hasSilentGhostEye(PlayerEntity player) {
        for (int i = 0; i < 6; i++) {
            ItemStack stack = PlayerEvents.getGhostSlotItem(player, i);
            if (!stack.isEmpty() && stack.getItem() == ModItems.SILENT_GHOST_EYE) {
                return true;
            }
        }
        return false;
    }
}